﻿using GUI.RLAServiceReference;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace GUI
{
    /// <summary>
    /// Interaction logic for UICreateUser.xaml
    /// </summary>
    public partial class UICreateUser : Window
    {
        private RealLifeServiceClient _client;

        public UICreateUser()
        {
            InitializeComponent();
            _client = new RealLifeServiceClient();
        }

        private void ButtonCancel(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void ButtonCreate(object sender, RoutedEventArgs e)
        {
            if (IsValidInput())
            {
                User user = new User
                {
                    FirstName = TxtFName.Text,
                    LastName = TxtLName.Text,
                    ProfileName = TxtPName.Text,
                    Email = TxtMail.Text,
                    Password = TxtPassword.Password,
                    Gender = RBtnMale.IsChecked == true ? 'M' : 'F'
                };

                switch (_client.CreateUser(user))
                {
                    case 1:
                        MessageBox.Show("Brugeren er nu oprettet");
                        this.Close();
                        break;

                    case -1:
                        MessageBox.Show("Der skete en fejl");
                        break;

                    case -2:
                        MessageBox.Show("Email er allerede registreret i systemet");
                        break;

                    case -3:
                        MessageBox.Show("Profilnavn er allerede registreret i systemet");
                        break;
                }
            }
        }

        private bool IsValidInput()
        {
            string[] args = { TxtFName.Text, TxtLName.Text, TxtPName.Text, TxtPassword.Password };

            bool valid = true;
            bool validMail = true;
            int index = 0;
            while (valid && index < args.Count())
            {
                if (args[index].Length < 1)
                {
                    valid = false;
                }
                index++;
            }

            if (!valid)
            {
                MessageBox.Show("Et eller flere felter mangler.");
            }
            else if (!Regex.IsMatch(TxtMail.Text, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"))
            {
                MessageBox.Show("Den indtastede e-mail addresse er ikke gyldig.");
                validMail = false;
            }
             
            return (valid && validMail);
        }
    }
}